//
//  BMHttpTool.m
//  砼车Demo
//
//  Created by apple on 16/1/7.
//  Copyright © 2016年 apple. All rights reserved.
//




NSString *const kDefaultMessage = @"数据加载中...";
NSString *const kBMNetworkRequestDomain = @"com.BlueMobi.chrain";
NSString *const kBMNetworkRequestErrorKey = @"kBMNetworkRequestErrorKey";

const NSUInteger count = 1024 *1024;


@interface BMHttpTool ()

@end


@implementation BMHttpTool

kSingletonM

- (AFHTTPSessionManager *)manager
{
    if (!_manager) {
        _manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:kServerPath]];
        NSMutableSet *contentTypes = [NSMutableSet setWithSet:_manager.responseSerializer.acceptableContentTypes];
        [contentTypes addObject:@"text/html"]; //添加类型
        _manager.responseSerializer.acceptableContentTypes = contentTypes;
        _manager.requestSerializer.timeoutInterval = 5.f;
    }
    return _manager;
}

-(void)getWith:(BMRequest *)request finish:(finishBlock)finish
{
//    [[BMHttpTool sharedInstance].manager GET:[kInterfacePath stringByAppendingPathComponent:request.path] parameters:request.params success:^(NSURLSessionDataTask *task, id responseObject) { //方法更新了
    
    [[BMHttpTool sharedInstance].manager GET:[kInterfacePath stringByAppendingPathComponent:request.path] parameters:request.params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        BMResponse *response = [self responseWithJson:responseObject];
        if (finish) {
            finish(response,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finish) {
            BMResponse *response = [BMResponse new];
            response.error = error;
            finish(response,error);
        }
    }];
}
- (BMResponse *)responseWithJson:(id)responseObj
{
    BMResponse *response = [BMResponse mj_objectWithKeyValues:responseObj];
    id result = responseObj[@"data"];
    if (result && ![result isKindOfClass:[NSNull class]] && [result isKindOfClass:[NSDictionary class]]) {
        NSDictionary *pageDict = result[@"page"];
        if (pageDict) {
            response.page = [BMPage mj_objectWithKeyValues:pageDict]; //更新
        }
        response.rawResult = result;
        response.emptyResult = NO;
    }else{
        response.emptyResult = YES;
    }
    return response;
}

- (void)postWith:(BMRequest *)request finish:(finishBlock)finish
{
    [[BMHttpTool sharedInstance].manager POST:[kInterfacePath stringByAppendingPathComponent:request.path] parameters:request.params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        BMResponse *response = [self responseWithJson:responseObject];
        if (finish) {
            finish(response,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BMResponse *response = [BMResponse new];
        response.emptyResult = YES;
        response.error = error;
        finish(response,error);
    }];
}

- (void)uploadWith:(BMRequest *)request finish:(finishBlock)finish
{
    
    [[BMHttpTool sharedInstance].manager POST:[kInterfacePath stringByAppendingPathComponent:request.path] parameters:request.params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [request.uploadData enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            [formData appendPartWithFileData:obj name:key fileName:@"a.jpg" mimeType:@"image/jpeg"];
        }];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        BMResponse *response = [self responseWithJson:responseObject];
        finish(response,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BMResponse *response = [BMResponse new];
        response.error = error;
        response.emptyResult = YES;
        finish(response,error);
    }];
    
}

@end
